// 封装购物车
import { defineStore } from "pinia";
import { ref } from "vue";
import { computed } from "vue";
import { insertCartAPI, updateNewList, delCartAPI } from "@/apis/cart.js";
import { useUserStore } from "./user.js";
export const useCartStore = defineStore(
  "cart",
  () => {
    // 定义购物车
    const cartList = ref([]);
    // 引入用户信息
    // 刷新数据
    const updateNewCartList = async () => {
      let res = await updateNewList();
      //    获取最新购物车数据
      cartList.value = res.result;
    };
    const userStore = useUserStore();
    // 判断是否登录
    const isLogin = computed(() => {
      return userStore.userInfo.token;
    });
    // 添加购物车操作
    const addCart = async (goods) => {
      const { skuId, count } = goods;
      // 登录
      if (isLogin.value) {
        // 登录之后的加入购车逻辑
        await insertCartAPI({ skuId, count });
        // //    获取最新购物车数据
        updateNewCartList();
      } else {
        // 未登录
        const item = cartList.value.find((item) => goods.skuId === item.skuId);
        if (item) {
          // 找到了
          item.count++;
        } else {
          // 没找到
          cartList.value.push(goods);
        }
      }
    };
    // 删除购物车
    const delCart = async (skuId) => {
      if (isLogin.value) {
        await delCartAPI([skuId]);
        //    获取最新购物车数据
        updateNewCartList();
      } else {
        const item = cartList.value?.filter((i) => i.skuId !== skuId);
        cartList.value = item;
      }
    };
    // 清空购物车
    const clearCart = () => {
      cartList.value = [];
    };
    // 计算总数量
    const allCount = computed(() => {
      return cartList.value.reduce((a, c) => a + c.count, 0);
    });
    // 计算总价
    // 计算总数量
    const allPrice = computed(() => {
      return cartList.value.reduce((a, c) => a + c.count * c.price, 0);
    });
    // 单选功能
    const singleCheck = (skuId, selected) => {
      // 通过skuId找到要修改的那一项 然后把它的selected修改为传过来的selected
      const item = cartList.value.find((item) => item.skuId === skuId);
      item.selected = selected;
    };
    // 是否全选
    const isAll = computed(() => {
      return cartList.value.every((i) => i.selected);
    });

    // 全选功能
    const selectedAll = (selected) => {
      cartList.value.forEach((element) => {
        element.selected = selected;
      });
      // // 通过skuId找到要修改的那一项 然后把它的selected修改为传过来的selected
      // const item = cartList.value.find((item) => item.skuId === skuId)
      // item.selected = selected
    };
    //   计算已选择数量
    const selecteCount = computed(() => {
      const items = cartList.value.filter((item) => item.selected);
      return items.reduce((a, c) => a + c.count, 0);
      //   console.log(items)
    });
    //   计算已选择商品价格
    const selectePrice = computed(() => {
      const items = cartList.value.filter((item) => item.selected);
      return items.reduce((a, c) => a + c.count * c.price, 0);
    });

    return {
      cartList,
      addCart,
      isAll,
      delCart,
      allCount,
      allPrice,
      singleCheck,
      selectedAll,
      selecteCount,
      selectePrice,
      clearCart,
      updateNewCartList,
    };
  },
  {
    persist: true,
  }
);
